package dbutils.dbutils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import entity.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;

public class Demo3 {
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    QueryRunner qr = new QueryRunner(dataSource);
    @Test
    public void findAll1() throws SQLException {
        String sql = "select * from user where id =?";
       User query = qr.query(sql, new BeanHandler<User>(User.class),2);
        System.out.println(query);
    }
    @Test
    public void findAll2() throws SQLException {
        String sql = "select * from user";
        List<User> list = qr.query(sql, new BeanListHandler<User>(User.class));
        for (User user : list) {
            System.out.println(user);
        }
    }
    @Test
    public void count() throws SQLException {
    String sql = "select count(*) from user";
        Object query = qr.query(sql, new ScalarHandler());
        System.out.println(query);
    }
}
